//
//  IndexController.m
//  linlee-iotios
//
//  Created by mengze on 2025/5/25.
//

#import <Foundation/Foundation.h>
#import "IndexController.h"
#import <Masonry/Masonry.h>
#import "UIColor+Hex.h"
#import "IndexGridView.h"

#define kTabWidth 80
#define kIndicatorHeight 2


@interface IndexController()

@property (nonatomic, strong) UIScrollView *tabScrollView;
@property (nonatomic, strong) UIView *indicatorView;
@property (nonatomic, strong) NSArray *tabTitles;
@property (nonatomic, assign) NSInteger selectedIndex;

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) NSMutableArray * tabButtonList;

//@property ( nonatomic, assign ) CGFloat statusBarHeight;
//
//// Objective-C获取状态栏高度
//self.statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
//


@property ( nonatomic, strong ) UIView * mainView;

@end


@implementation IndexController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;

    
    self.tabButtonList = [[NSMutableArray alloc] init];

    self.view.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
    self.tabTitles = @[@"白云门店", @"黄埔门店", @"天鹅门店", @"天河门店", @"越秀门店", @"越秀门店", @"越秀门店", @"越秀门店", @"越秀门店"];
    
    // 创建滚动容器
    self.tabScrollView = [[UIScrollView alloc] init];
    [self.tabScrollView setBackgroundColor:[UIColor colorWithHexString: @"ffffff"]];
    self.tabScrollView.showsHorizontalScrollIndicator = NO;

    [self.view addSubview:self.tabScrollView];
    
    
    self.scrollView = [[UIScrollView alloc] init];
    [self.scrollView setBackgroundColor:[UIColor whiteColor]];
    self.contentView = [[UIView alloc] init];
       
   [self.view addSubview:_scrollView];
   [self.scrollView addSubview:_contentView];
    

    // 指示器
    self.indicatorView = [[UIView alloc] init];
    self.indicatorView.backgroundColor = [UIColor greenColor];
    [self.tabScrollView addSubview:self.indicatorView];
    
    // 布局容器
    self.tabScrollView.backgroundColor = [UIColor whiteColor];
    [self.tabScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.height.equalTo(@44);
    }];
    
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        make.top.equalTo(self.tabScrollView.mas_bottom);
    }];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_scrollView);
        make.width.equalTo(_scrollView);
    }];
    
    // 创建标签按钮
    UIView *lastView;
    for (NSInteger i = 0; i < self.tabTitles.count; i++) {
        UIButton *tabButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [tabButton setTitle:self.tabTitles[i] forState:UIControlStateNormal];
        [tabButton setTitleColor:[UIColor colorWithHexString: @"999"] forState:UIControlStateNormal];
        [tabButton setTitleColor:[UIColor colorWithHexString:@"#000"] forState:UIControlStateSelected];
        [tabButton setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
        tabButton.font = [UIFont systemFontOfSize: 14];
        tabButton.tag = 100 + i;
        [tabButton addTarget:self action:@selector(tabButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabButtonList addObject:tabButton];
        [self.tabScrollView addSubview:tabButton];
        
        [tabButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.tabScrollView);
            make.height.equalTo(@44);
            make.width.equalTo(@(kTabWidth));
            if (lastView) {
                make.left.equalTo(lastView.mas_right);
            } else {
                make.left.equalTo(self.tabScrollView);
            }
        }];
        
        lastView = tabButton;
    }
    
    // 设置内容大小
    [self.tabScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lastView.mas_right);
    }];
    
    // 默认选中第一个
    [self selectTabAtIndex:0];
    [self setupGridItems];
}

- (void)tabButtonClicked:(UIButton *)sender {
    [self selectTabAtIndex:sender.tag - 100];
}

- (void)selectTabAtIndex:(NSInteger)index {
    self.selectedIndex = index;
    
    
    for( int i=0; i<self.tabButtonList.count; i++ ){
        UIButton * button = self.tabButtonList[i];
        if( button.selected ){
            button.selected = false;
            [button setFont:[UIFont systemFontOfSize:14]];
        }
    }
    
    UIButton *selectedButton = [self.tabScrollView viewWithTag:100 + index];
    selectedButton.selected = true;
    [selectedButton setFont:[UIFont boldSystemFontOfSize:18]];
    
    // 更新指示器位置
    [self.indicatorView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tabScrollView).offset(42);
        make.centerX.equalTo(selectedButton);
        make.width.equalTo(@(kTabWidth - 20));
        make.height.equalTo(@(3));
    }];
    
    // 动画效果
    [UIView animateWithDuration:0.3 animations:^{
        [self.view layoutIfNeeded];
    }];
    
    // 这里可以添加切换内容的逻辑
}



- (void)setupGridItems {
    NSMutableArray *itemViews = [NSMutableArray array];
    
    // 创建10个示例item
    for (int i = 0; i < 49; i++) {
        IndexGridView *item = [[IndexGridView alloc] init];
        item.titleLabel.text = [NSString stringWithFormat:@"奶茶机%d", i+1];
        item.detailLabel.text = [NSString stringWithFormat:@"正常"];
        item.imageView.image = [UIImage imageNamed:@"device"];
        [item setStatus: i%2];
        [self.contentView addSubview:item];
        [itemViews addObject:item];
    }
    
    // 布局网格
    UIView *lastView = nil;
    for (int i = 0; i < itemViews.count; i++) {
        IndexGridView *item = itemViews[i];
        
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.contentView).multipliedBy(0.5).offset(-15);
            make.height.equalTo(@120);
            
            if (i % 2 == 0) { // 左边item
                make.left.equalTo(self.contentView).offset(10);
            } else { // 右边item
                make.left.equalTo(lastView.mas_right).offset(10);
            }
            
            if (i < 2) { // 第一行
                make.top.equalTo(self.contentView).offset(10);
            } else { // 其他行
                make.top.equalTo(((IndexGridView *)itemViews[i-2]).mas_bottom).offset(10);
            }
        }];
        
        lastView = item;
    }
    
    // 设置contentView底部约束
    if (lastView) {
        [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-10);
        }];
    }
}
@end
